import util from '@/libs/util.js' // 或者通过bus总线引入亦可
import Vue from 'vue'

import util2 from '@/libs/util2.js' // 或者通过bus总线引入亦可

const projectName = process.env.NODE_ENV == 'development' ? '' : '/tower-lease-report-service';

const projectName1 = process.env.NODE_ENV == 'development' ? '' : '/wirautoopenservice';


const projectName2 = process.env.NODE_ENV == 'development' ? '' : '/sysbasequery';

// 调用参数带@RequestBody 的post接口
export const getInfo = () => {
    return util.ajaxPostData('/api/getInfo', {
        pra: 'ssss'
    })
}

// get需额外提供方法声明
export const getInfoThroughGet = () => {
    return util.ajaxMethodWidthParams('/api/getInfo', 'get')
}

// 默认post方式 ,后台不带@RequestBody
export const getInfoWithoutBody = () => {
    return util.ajaxGetData('/api/getInfoCommon', {
        pra: '不带requestBody的请求'
    })
}
// 调用参数带@RequestBody 的post接口
export const getUrl = (url, params) => {
    return util.ajaxPostData(`${projectName}` + url, params)
}

// 调用参数带@RequestBody 的post接口
export const getUrl1 = (url, params) => {
    return util2.ajaxPostData(`${projectName1}` + url, params)
}

// 调用参数带@RequestBody 的post接口
export const getUrl2 = (url, params) => {
    return util2.ajaxPostData(`${projectName2}` + url, params)
}


// 默认post方式 ,后台不带@RequestBody
export const getGUrl = (url,params) => {
    return util.ajaxMethodWidthParams(`${projectName}` + url, 'get',params)
}

// 默认post方式 ,后台不带@RequestBody
export const getGUrl1 = (url,params) => {
    return util2.ajaxMethodWidthParams(`${projectName1}` + url, 'get',params)
}




export const exportFile = (url, param) => {
    util.downloadPostData(`${projectName}` + url, param).then(res => {
        console.log(res,"res")
        var title = res.headers['content-disposition'].substring(20)
        const blob = new Blob([res.data])
        // 解决中文名乱码
        let iconv = require('iconv-lite')
        iconv.skipDecodeWarning = true;
        let fileName = iconv.decode(title, 'utf-8');
        console.log(fileName, "fileName")
        const elink = document.createElement('a')
        if (fileName.indexOf(".xlsx") != -1) {
            elink.setAttribute('download', fileName)
        } else {
            elink.setAttribute('download', fileName )
        }

        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
    }).catch(err => {
        Vue.prototype.$message({
            message: '导出失败',
            type: 'error'
        })
    })
}







export const exportFile1 = (url, param) => {
    util2.downloadPostData(`${projectName1}` + url, param).then(res => {
        console.log(res,"res")
        var title = res.headers['content-disposition'].substring(20)
        const blob = new Blob([res.data])
        // 解决中文名乱码
        let iconv = require('iconv-lite')
        iconv.skipDecodeWarning = true;
        let fileName = iconv.decode(title, 'utf-8');
        console.log(fileName, "fileName")
        const elink = document.createElement('a')
        if (fileName.indexOf(".xlsx") != -1) {
            elink.setAttribute('download', fileName)
        } else {
            elink.setAttribute('download', fileName )
        }

        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
    }).catch(err => {
        Vue.prototype.$message({
            message: '导出失败',
            type: 'error'
        })
    })
}

